const http = require('http')
const fs = require('fs')
const server = http.createServer()

server.on('request', function (req, res) {
    if (req.url == '/') {
        fs.readdir('./file', function (err, data) {
            console.log(data);
            fs.readFile('./file/index.html', function (indexError, indexData) {
                console.log(000)
                let html = indexData.toString().replace('{-_-}', data.map(iteam => `<li>${iteam}</li>`).join(''))
                res.setHeader('Content-Type', 'text/html', 'charset=utf-8')
                res.end(html)
            })
        })
        
        console.log(123)
    } else {
        fs.readFile('./file' + req.url, function (err, data) {
            if (err) {
                res.setHeader('Content-Type', 'text/html', 'charset=utf-8')
                res.end('输入地址有误')
            } else {
                res.end(data)
            }
        })
    }
})

server.listen(3000, function () {
    console.log('监听')
})